

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:my_chat/entities/friend.dart';


class PosAndSize {
  Size size;
  Offset offset;

  PosAndSize(this.size, this.offset);
}

class FriendGroup extends StatelessWidget {
  GlobalKey firstLetterKey = GlobalKey();
  MapEntry<String, List<Friend>> entry;

  FriendGroup({super.key, required this.entry});

   PosAndSize getLetterPositionAndSize() {
    RenderBox renderBox = firstLetterKey.currentContext!.findRenderObject() as RenderBox;
  
  
    return PosAndSize(
      firstLetterKey.currentContext!.size!,
      renderBox.localToGlobal(Offset.zero)
    );
  }

  Widget _renderFriendList() {
    List<Widget> children = entry.value.asMap().keys.map((index) {
      Friend item = entry.value[index];

      return Column(
        children: [
          const SizedBox(
            height: 10,
          ),
          Row(
            children: [
              // 头像
              Container(
                width: 40,
                height: 40,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(6),
                    color: Colors.grey,
                    image: DecorationImage(
                        image: NetworkImage(item.avatar ?? ''),
                        fit: BoxFit.cover)),
              ),
              Container(
                padding: const EdgeInsets.only(left: 20),
                child: Text(item.name ?? ''),
              )
            ],
          ),
          // 底部的线条
          Container(
            height: 10,
            margin: EdgeInsets.only(left: index == entry.value.length - 1 ? 0 : 50),
            decoration: const BoxDecoration(
                border: Border(
                    bottom: BorderSide(
                        color:   Color.fromARGB(50, 100, 100, 100),
                        width: 1))),
          )
        ],
      );
    }).toList();

    return Container(
      padding: const EdgeInsets.only(left: 10),
      child: Column(
        children: children,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          // 组标题
          Container(
            key: firstLetterKey,
            alignment: Alignment.centerLeft,
            height: 40,
            padding: const EdgeInsets.only(left: 10),
            width: double.infinity,
            child: Text(
              entry.key,
              style: const TextStyle(fontSize: 16),
            ),
          ),
          _renderFriendList()
        ],
      ),
    );
  }
}
